<!--
 * @Author: 詹真琦(legendryztachi@gmail.com)
 * @Date: 2021-11-20 14:28:14
 * @LastEditors: 詹真琦(legendryztachi@gmail.com)
 * @LastEditTime: 2022-02-13 18:26:26
 * @FilePath: \code\cwp-front\pc-collaboration\src\components\opinion-turn-area\components\item\index.vue
 * @Description: 附言或者评论
-->
<template>
    <div class="opinion-turn-item">
        <PostscriptArea v-if="postscriptList.length" :data="postscriptList" />
        <BaseComment
            v-if="opinion.comments.length"
            :data="opinion"
            :canPraise="false"
            :canAddComment="false"
        />
    </div>
</template>
<script>
    //发起人附言
    import PostscriptArea from '@/components/postscript-area';
    //处理人意见
    import BaseComment from '@/components/opinion-area/base-comment.vue';
    export default {
        name: 'OpinionTurnItem',
        components: { PostscriptArea, BaseComment },
        props: {
            //当前数据
            data: {
                type: Object,
            },
        },
        data() {
            return {
                //附言列表
                postscriptList: [],
                //意见对象
                opinion: { comments: [] },
            };
        },
        watch: {
            //监听评论加载了更多，就重新绘制
            'data.comments'(v) {
                this.formatList(v);
            },
        },
        created() {
            const { praiseTotal, commentTotal } = this.data;
            this.formatList(this.data.comments);
            this.opinion.commentTotal = commentTotal;
            this.opinion.praiseTotal = praiseTotal;
        },
        methods: {
            /**
             * @description: 格式化列表
             * @param {Array} commentList 评论列表
             * @return {*}
             */
            formatList(commentList) {
                const postscriptList = [];
                const comments = [];
                //分配附言和意见列表
                commentList.forEach((d) => {
                    if (d.ctype === -1) {
                        postscriptList.push(d);
                    } else {
                        comments.push(d);
                    }
                });
                this.postscriptList = postscriptList;
                this.opinion.comments = comments;
            },
        },
    };
</script>
<style lang="scss" scoped></style>
